<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 19, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "mvc.AbstractController" );

class AbstractBackEndController extends AbstractController {

	function &_getReadOnlyFieldInfo($inputName) {
		RCoreImporter::cls('html.fieldInfo.BasicHtmlFieldInfo');
		$info = new BasicHtmlFieldInfo($inputName);
		$info->setContentOnly();
		return $info;
	}
	
	/**
	 * Get the fieldInfo object for image
	 * @param $inputName
	 * @param $allowedFileType
	 * @return FlashFileHtmlFieldInfo
	 */
	function &_getImageFieldInfo($inputName, $allowedFileType = '*.gif, *.jpg, *.png') {
		RCoreImporter::cls('html.fieldInfo.FlashFileHtmlFieldInfo');
		$info = new FlashFileHtmlFieldInfo($inputName);
		$info->setSupportedFilteType($allowedFileType);
		return $info;
	}
	
	/**
	 * Get the fieldInfo obejct for password field
	 * @param $inputName
	 * @param $isNew
	 * @return PasswordHtmlFieldInfo
	 */
	function &_getPasswordFieldInfo($inputName, $isNew = true){
		RCoreImporter::cls('html.fieldInfo.PasswordHtmlFieldInfo');
		$info = new PasswordHtmlFieldInfo($inputName);
		if($isNew){
			$info->setValidateCssCls("required");
		}
		else {
			$info->setValidateCssCls("none");
		}
		return $info;			
	}
	
	/**
	 * Get the fieldInfo obejct for hidden field
	 * @param $inputName
	 * @return HiddenHtmlFieldInfo
	 */
	function &_getHiddenFieldInfo($inputName){
		RCoreImporter::cls('html.fieldInfo.HiddenHtmlFieldInfo');
		$info = new HiddenHtmlFieldInfo($inputName);
		return $info;			
	}
	
	/**
	 * Get the fieldInfo obejct for date field
	 * @param $inputName
	 * @return DateHtmlFieldInfo
	 */
	function &_getDateHtmlFieldInfo($inputName){
		RExtImporter::cls('jscalendar.JsCalendarDateHtmlFieldInfo');
		$info = new JsCalendarDateHtmlFieldInfo($inputName);
		return $info;			
	}
	
	/**
	 * Get the fieldInfo obejct for static class field
	 * 
	 * @param $inputName
	 * @param $classPath 'const.TEST_TYPE'
	 * @return StaticComboboxHtmlFieldInfo
	 */
	function &_getStaticComboboxFieldInfo($inputName, $classPath) {
		RClassesImporter::cls($classPath);
		RCoreImporter::cls('html.fieldInfo.StaticComboboxHtmlFieldInfo');
		$info = new StaticComboboxHtmlFieldInfo($inputName);
		
		$class = substr($classPath, RStringHelper::lastIndexOf($classPath, '.'));
		$info->setClass($class);
		return $info;				
	}

	/**
	 * Get the sqlTreeComboboxHtmlField Info
	 * @param $inputName
	 * @param $arrayOrObject
	 * @param $sql
	 * @param $parentId
	 * @param $orderBy
	 * @return SqlTreeComboboxHtmlFieldInfo
	 */
	function &_getSqlTreeComboboxHtmlFieldInfo($inputName, $sql, $parentId, $orderBy='') {
		RCoreImporter::cls('html.fieldInfo.SqlTreeComboboxHtmlFieldInfo');
		$info = new SqlTreeComboboxHtmlFieldInfo($inputName);
		$info->setSQL($sql, $parentId, $orderBy);
		return $info;
	}

	/**
	 * Get the SqlComboboxHtmlFieldInfo Info
	 * @param $inputName
	 * @param $arrayOrObject
	 * @param $sql
	 * @param $parentId
	 * @param $orderBy
	 * @return SqlTreeComboboxHtmlFieldInfo
	 */
	function &_getSqlComboboxHtmlFieldInfo($inputName, $sql) {
		RCoreImporter::cls('html.fieldInfo.SqlComboboxHtmlFieldInfo');
		$info = new SqlComboboxHtmlFieldInfo($inputName);
		$info->setSQL($sql);
		return $info;
	}

	/**
	 * Get the RichTextArea fieldInfo 
	 * @param $fieldName
	 * @return RichTextHtmlFieldInfo
	 */
	function &_getRichTextHtmlFieldInfo($fieldName) {
		RCoreImporter::cls('html.fieldInfo.RichTextHtmlFieldInfo');
		$field = new RichTextHtmlFieldInfo($fieldName);
		return $field;
	}
	function &_getFileFieldInfo($id) {
		RClassesImporter::cls('html.PsdFileHtmlFieldInfo');
		$info = new PsdFileHtmlFieldInfo($id);
		return $info;
	}

	
	
	///////////////////////////////////////////////////////////////////////////////////

	/**
	 * Function for general processing the edit task
	 *
	 * @param $dto AbstractDTO
	 * @return FormFieldsControl
	 */
	function &_getEditFormAndPrepareEditTask($dto, $name)
	{
		$this->addToolbarButton ( "save" );
		//$this->addToolbarButton ( "apply" );
		$this->addToolbarButton ( "cancel" );
		$msg = SysRequest::getVar ( "msg" );
		
		if ($dto->checkKeyNotDefault()) {
			$this->setTitle ( "Edit {$name}" );
		}
		else {
			$this->setTitle ( "New {$name}" );
		}

		RCoreImporter::cls( "html.FormFieldsControl" );			
		$formData = new FormFieldsControl ( $dto );
		
		return $formData;
	}
	
	/**
	 * Set the files value for the the file field in the dto
	 * @param $fieldName string
	 * @param $dto AbtractDTO
	 * @return unknown_type
	 */
	function _setFileValue($fieldName, &$dto, $pathToUpload, $arrPost, $allowedFileType = '*.gif, *.jpg, *.png') {
		$ctrl = $this->_getImageFieldInfo($fieldName, $allowedFileType);
		$dto->$fieldName = $ctrl->getUploadedFilePath($pathToUpload, $arrPost);
	}
}
?>